var Plot = Plot || {};
var Tree2d = Tree2d || {};

/***************************************************************
 *                    DEFAULT MOUSE EVENTS
 **************************************************************/

/**
 * Default function called when 'mousedown' event is fired.
 * You can redefined your own function until it accepts following parameters.
 * @param {Object} coord Object with 'x' and 'y' fields corresponding to the mouse
 *  position on the plot.
 * @param {Event} event Original event fired.
 */
Plot.prototype.mousedown = function (coord, event) {
    'use strict';
    if (event.shiftKey) {
        this.startSelectArea(coord);
    }
};

/**
 * Default function called when 'mousemove' event is fired.
 * You can redefined your own function until it accepts following parameters.
 * It is possible to access to the mouse coordinate of event 'mousedown'
 * with 'this.coordDown'.
 * @param {Object} coord Object with 'x' and 'y' fields corresponding to the mouse
 * position on the plot.
 * @param {Event} event Original event fired.
 */
Plot.prototype.mousemove = function (coord, event) {
    'use strict';
    if (this.coordDown === undefined) {
        return;
    }
    var oldCoord = this.coordDown;
    var newCoord = coord;
    if (event.shiftKey) {
        this.updateSelectArea(oldCoord, newCoord);
    } else {
        this.cancelSelectArea();
        this.translateAxis(newCoord.x - oldCoord.x, newCoord.y - oldCoord.y);
    }
};

/**
 * Default function called when 'mouseup' event is fired.
 * You can redefined your own function until it accepts following parameters.
 * It is possible to access to the mouse coordinate of event 'mousedown'
 * with 'this.coordDown'.
 * @param {Object} coord Object with 'x' and 'y' fields corresponding to the mouse
 * position on the plot.
 * @param {Event} event Original event fired.
 */
Plot.prototype.mouseup = function (coord, event) {
    'use strict';
    var oldCoord = this.coordDown;
    var newCoord = coord;
    if (event.shiftKey) {
        this.endSelectArea(oldCoord, newCoord);
    } else {
        this.translateAxis(newCoord.x - oldCoord.x, newCoord.y - oldCoord.y);
    }
};

/**
 * Default function called when 'mousewheel' event is fired.
 * You can redefined your own function until it accepts following parameters.
 * @param {number} direction  1 or -1 following the wheel direction.
 * @param {Object} coord Object with 'x' and 'y' fields corresponding to the mouse
 * position on the plot.
 * @param {Event} event Original event fired.
 */
Plot.prototype.mousewheel = function (direction, coord, event) {
    'use strict';
    var f = 1 + direction * 0.1;
    this.zoomAxis(coord, f);
};

/**
 *  Default function called when 'click' event is fired.
 *  'click' event corresponds to 'mousedown' and 'mouseup' events fired at
 *  the same location.
 *  You can redefined your own function until it accepts following parameters.
 *  It is possible to access to the mouse coordinate of event 'mousedown'
 *  with 'this.coordDown'.
 * @param {Object} coord
 *  Object with 'x' and 'y' fields corresponding to the mouse
 *  position on the plot.
 * @param {Event} event
 *  Original event fired.
 * @example
 *  // Defined an action when click event occurs
 *  myPlot.click = function (coord, event) {
 *    alert('click at point(' + coord.x + ', ' + coord.y + ').')
 *  };
 */
Plot.prototype.click = function (coord, event) {
    'use strict';
    var c = this.getClosestPoint(coord.x, coord.y, false);
    if (c) {
        this.setCursor(c.x, c.y);
    }
};

/**
 * Default function called when an area has been selected.
 * You can redefined your own function until it accepts following parameters.
 * @param {number} x1
 *  x first coordinate.
 * @param {number} y1
 *  y first coordinate.
 * @param {number} x2
 *  x second coordinate.
 * @param {number} y2
 *  y second coordinate.
 * @example
 * // Defined an action when an area is selected
 * myPlot.selectarea = function (x1, y1, x2, y2) {
 *   alert('You selected area from ('+x1+', '+y1+') to ('+x2+', '+y2+').')
 * };
 */
Plot.prototype.selectarea = function (x1, y1, x2, y2) {
    'use strict';
    this.setAxis([x1, y1, x2, y2]);
};